package Main;

import java.io.IOException;
import java.text.BreakIterator;

import javax.swing.JOptionPane;
import javax.swing.UnsupportedLookAndFeelException;
import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;

import Lexicon.Lexicon;
import Lexicon.MyNode;
import Lexicon.WordType;

public class ArToEn {//TODO if ar[i] == defined && ar[i+1] == defined the replace also with verbal 
	//TODO quick translation , if didn't find a word , replace it with  itself
	String [] arwords , enwords;
	String arText ,enTxt = new String();
	Lexicon lex ;
	MyNode[] n ;
	//int lineInd ;
	public ArToEn(String text)
	{
		arText = text ;
		//lineInd = arText.indexOf("\n") ;
		arwords = arText.split("\\s"/*,lineInd*/);//TODO : make use of BreakIterator.getSentenceInstance(currentLocale);
	   
		try {
			lex = new Lexicon();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		}
		enwords = new String[arwords.length];
	    n = new MyNode[arwords.length];
	}
	//TODO split over lines   
	public String quickTranslation()
	{
		
		for(int i = 0 ; i < arwords.length ; i++)
		{
			try{//try to find the word or its root
				n[i] = lex.find(arwords[i]) ;
				enwords[i] = n[i].getEnglishWord() ;
				
			}catch(NullPointerException ne)//faild to find word/root
			{
				enwords[i] =//"("+
				arwords[i]
				       // +")"
				        ;
			}
			enTxt += enwords[i] ;
			enTxt += " " ;
			if(".".equals(arwords[i]) || arwords[i].endsWith("."))
				enTxt += "\n" ;
		}
		return enTxt  ;
	}
	public String accurateTranslation() throws SAXException, IOException, ParserConfigurationException
	{
		MyNormalizer norm = new MyNormalizer(arText) ;
		MyNode temp ;
		try {
			if(norm.supported() == true)
			{
				for(int i = 0 ; i < arwords.length ; i++)
				{
				n[i] = Lexicon.currentTree.find(arwords[i]);
				}
				for(int i = 0 ; i < n.length ; i++)
				{
					if(i < n.length-1)
				if(n[i].getType() == WordType.NORMAL_VERB && (n[i+1].getType() == WordType.DEFINED_NOUN /*|| n[i+1].getType() == WordType.INDETERMINATE_NOUN*/))
						{
							temp = n[i];
							n[i] = n[i+1] ;
							n[i+1] = temp ;	
							}
						enwords[i] = n[i].getEnglishWord() ;
						enTxt += enwords[i] ;
						enTxt += " " ;
						if(".".equals(arwords[i]) || arwords[i].endsWith("."))
							enTxt += "\n" ;
						}
			}
		} catch (UnsupportedLookAndFeelException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return enTxt  ;
	}

}
